#define GLFW_INCLUDE_VULKAN
#include "window-mgr.h"
#include "../gfx/gfx-mgr.h"

const int MIN_WIDTH = 640;
const int MIN_HEIGHT = 360;

WindowMgr::WindowMgr() : _window(nullptr)
{
}
WindowMgr::~WindowMgr()
{
}
WindowMgr *WindowMgr::getInstance()
{
    static WindowMgr instance;
    return &instance;
}

void WindowMgr::init()
{
    if (!glfwInit())
    {
        return;
    }

    // 获取显示器的视频模式
    GLFWmonitor *monitor = glfwGetPrimaryMonitor();
    // 获取显示器的视频模式
    const GLFWvidmode *mode = glfwGetVideoMode(monitor);
    float width = mode->width < MIN_WIDTH ? MIN_WIDTH : mode->width;
    float height = mode->height < MIN_HEIGHT ? MIN_HEIGHT : mode->height;

    width = MIN_WIDTH;
    height = MIN_HEIGHT;

    View::viewWidth = width;
    View::viewHeight = height;
    glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
    // const int C_GLFW_TRANSPARENT_FRAMEBUFFER = 131082;
    // glfwWindowHint(C_GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE);

    // 创建窗口
    this->_window = glfwCreateWindow(width, height, "", nullptr, nullptr);
    if (!this->_window)
    {
        glfwTerminate();
        return;
    }
    glfwSetWindowSizeLimits(this->_window, MIN_WIDTH, MIN_HEIGHT, GLFW_DONT_CARE, GLFW_DONT_CARE);
    glfwSetWindowUserPointer(this->_window, this);
    glfwSetCursorPosCallback(this->_window, WindowMgr::cursorPosCallback);
    glfwSetMouseButtonCallback(this->_window, WindowMgr::mouseButtonCallback);
    glfwSetWindowSizeCallback(this->_window, WindowMgr::windowSizeCallback);
}
GLFWwindow *WindowMgr::getWindow()
{
    return this->_window;
}
void WindowMgr::tick()
{
    while (!glfwWindowShouldClose(this->_window))
    {
        glfwPollEvents();
    }
}

void WindowMgr::cursorPosCallback(GLFWwindow *window, double xpos, double ypos)
{
    WindowMgr *manager = static_cast<WindowMgr *>(glfwGetWindowUserPointer(window));
    if (manager)
    {
        manager->onCursorPos(xpos, ypos); // 调用非静态处理函数
    }
}
void WindowMgr::mouseButtonCallback(GLFWwindow *window, int button, int action, int mods)
{
    WindowMgr *manager = static_cast<WindowMgr *>(glfwGetWindowUserPointer(window));
    if (manager)
    {
        manager->onMouseButton(button, action, mods); // 调用非静态处理函数
    }
}
void WindowMgr::windowSizeCallback(GLFWwindow *window, int width, int height)
{
    WindowMgr *manager = static_cast<WindowMgr *>(glfwGetWindowUserPointer(window));
    if (manager)
    {
        manager->onWindowSize(width, height); // 调用非静态处理函数
    }
}
void WindowMgr::onCursorPos(double xpos, double ypos)
{
}
void WindowMgr::onMouseButton(int button, int action, int mods)
{
    std::cout << "WindowMgr::onMouseButton:" << button << "   " << action << "   " << mods << std::endl;
}
void WindowMgr::onWindowSize(int width, int height)
{
    // std::cout << "WindowMgr::onWindowSize:" << width << "   " << height << std::endl;
    // 更新布局
    View::viewWidth = width;
    View::viewHeight = height;
    View::flag++;
    Event::getInstance().emit();
}
void WindowMgr::destroy()
{
    glfwDestroyWindow(this->_window);
    glfwTerminate();
}
